In [1]:
from __future__ import division
from functions import *
from utils import *
from gevi_classes import *

%matplotlib inline
%load_ext autoreload
%autoreload 2
In [2]:
%%javascript
IPython.OutputArea.auto_scroll_threshold = 1000;
In [3]:
# instantiate utility class
gr = Graph()

Load DATA

Data collected in 5 X 1min, with 1min pause in between 2921 images obtained in 60s

In [4]:
username = os.path.expanduser('~').split('/')[-1]
if username == "GP1514":
    print("At Imperial")
    mouseAPath = '/Volumes/DATA/DATA/Equalized Separation/2014 Oct 27/'
    mouseBPath = '/Volumes/DATA/DATA/Equalized Separation/2014 Oct 22/'
    mouseCPath = '/Volumes/DATA/DATA/Equalized Separation/2014 Oct 22/'
else:
    print("Using laptop")
    mouseAPath = '/Users/guillaume/Projects/GEVI-DATA/2014 Oct 27/'
    mouseBPath = '/Users/guillaume/Projects/GEVI-DATA/2014 Oct 22/'
    mouseCPath = '/Users/guillaume/Projects/GEVI-DATA/2014 Oct 28/'
At Imperial
In [5]:
discard = {
    'MouseA': 
    {
        3 : [0,1], 
        4 : [1,2], 
        5 : [2],
        6 : [2,3,4]
    }, 
    'MouseB' : 
    {
        3 : [2,4,5],
        4 : [1,2]
    }
}
# my choice
discard = {
    'MouseA': 
    {
        3 : [0,1,3], 
#         4 : [], 
#         5 : [],
        6 : [1,2,3,4]
    }, 
    'MouseB' : 
    {
        3 : [1,2,3,4],
        4 : [0]
    },
    'MouseC' :
    {}
}
In [6]:
mouseA = Mouse('mouseA', mouseAPath, [3,4,5,6],discard['MouseA'] )
mouseB = Mouse('mouseB', mouseBPath, [2,3,4,5],discard['MouseB'] )
mouseC = Mouse('mouseC', mouseCPath, [4],discard['MouseC'] )
In [7]:
mouseA.loadData()
mouseB.loadData()
mouseC.loadData()
In [8]:
mouseB.experiments[1].repeats[0].setInfo('End of transition, then discard')
mouseB.experiments[2].repeats[0].setInfo('Transition to anesthesia')
mouseA.experiments[0].repeats[2].setInfo('Keep')
mouseA.experiments[2].repeats[1].setInfo('Transition to desynchronization: short periods of silence trigger recovery of hemodynamics')
mouseA.experiments[3].repeats[1].setInfo('Woke up - Discard')
mouseA.experiments[3].repeats[2].setInfo('Discard')
mouseA.experiments[3].repeats[3].setInfo('Discard')
mouseA.experiments[3].repeats[4].setInfo('Discard')
mouseA.getInfo()
mouseB.getInfo()
 - experiment 3 has 2 valid repeats: 
	 - Repeat 0 (not valid)
	 - Repeat 1 (not valid)
	 - Repeat 2 (valid): Keep
	 - Repeat 3 (not valid)
	 - Repeat 4 (valid)
 - experiment 4 has 5 valid repeats: 
	 - Repeat 0 (valid)
	 - Repeat 1 (valid)
	 - Repeat 2 (valid)
	 - Repeat 3 (valid)
	 - Repeat 4 (valid)
 - experiment 5 has 5 valid repeats: 
	 - Repeat 0 (valid)
	 - Repeat 1 (valid): Transition to desynchronization: short periods of silence trigger recovery of hemodynamics
	 - Repeat 2 (valid)
	 - Repeat 3 (valid)
	 - Repeat 4 (valid)
 - experiment 6 has 1 valid repeats: 
	 - Repeat 0 (valid)
	 - Repeat 1 (not valid): Woke up - Discard
	 - Repeat 2 (not valid): Discard
	 - Repeat 3 (not valid): Discard
	 - Repeat 4 (not valid): Discard
 - experiment 2 has 5 valid repeats: 
	 - Repeat 0 (valid)
	 - Repeat 1 (valid)
	 - Repeat 2 (valid)
	 - Repeat 3 (valid)
	 - Repeat 4 (valid)
 - experiment 3 has 1 valid repeats: 
	 - Repeat 0 (valid): End of transition, then discard
	 - Repeat 1 (not valid)
	 - Repeat 2 (not valid)
	 - Repeat 3 (not valid)
	 - Repeat 4 (not valid)
 - experiment 4 has 4 valid repeats: 
	 - Repeat 1 (valid): Transition to anesthesia
	 - Repeat 2 (valid)
	 - Repeat 3 (valid)
	 - Repeat 4 (valid)
 - experiment 5 has 5 valid repeats: 
	 - Repeat 0 (valid)
	 - Repeat 1 (valid)
	 - Repeat 2 (valid)
	 - Repeat 3 (valid)
	 - Repeat 4 (valid)
In [9]:
# mouseA.experiments[0].repeats.mRatio

Plot Data

Mouse A

In [10]:
gr.plotHV(mouseA)

Mouse B

In [11]:
gr.plotHV(mouseB)

Mouse C

In [12]:
# gr.plotHV(mouseC)

Transfer functions

$V * \alpha = H$

$\mathscr{F}$ transform : $\hat{V}.\hat{\alpha} = \hat{H}$

$\alpha = \mathscr{F}^{-1} \frac{\hat{H}}{\hat{V}}$

ALPHA CURVE FITTING

$\alpha(t)=\frac{\tau_0}{t+0.1} - \frac{t}{\tau_1}*e^{\tau_3-\frac{t}{\tau_2}}$

Mouse A

In [13]:
def meanCorH(mouse):
    corH = np.mean(flatten(mouse.experiments.corH))
    return corH

def meanCorR(mouse):
    corR = np.mean(flatten(mouse.experiments.corR))
    return corR
    
# optimization for mouseA
def fn(p_est):
    alpha = [guess_function(xi,p_est[0],p_est[1],p_est[2]) for xi in x]
    gr.computeCorr(mouseA, alpha)
    corR = np.mean(flatten(mouseA.experiments.corR))
    res = 1 - corR
#     print('%.2f \t\t t0:%.5f \t t1:%.5f \t t2:%.5f \t t3:%.5f'%(res, p_est[0],p_est[1],p_est[2], p_est[3]))
    return res

def fnH(p_est):
    alpha = [guess_function(xi,p_est[0],p_est[1],p_est[2]) for xi in x]
    gr.computeCorr(mouseA, alpha)
    corH = np.mean(flatten(mouseA.experiments.corH))
    res = 1 - corH
#     print('%.2f \t\t t0:%.5f \t t1:%.5f \t t2:%.5f \t t3:%.5f'%(res, p_est[0],p_est[1],p_est[2], p_est[3]))
    return res

def computeAndPlotCorr(mouse, alpha):
        for i,exp in enumerate(mouse.experiments):
            for j,rep in enumerate(exp.repeats):
                gr.plotRModel(mouse, exp = i, rep = j,alpha = alpha )
                gr.plotHModel(mouse, exp = i, rep = j,alpha = alpha )
                gr.corrCoeff(mouse, exp = i, rep = j,alpha = alpha )
                
x = np.real(xax(mouseA.experiments[0].repeats[0].mRatio, 60000))
In [14]:
# filtering parameters
mouseA.minFreqAlpha = 1
mouseA.maxFreqAlpha = 200
mouseB.minFreqAlpha = 1
mouseB.maxFreqAlpha = 200
mouseA.window = 10
gr.fHmax = 30
gr.fRmax = 200

Tranfer functions mouse A

In [15]:
gr.plotTF(mouseA)
In [16]:
gr.plotmTF(mouseA)

Transfer functions mouse B

In [17]:
gr.plotTF(mouseB)
In [18]:
gr.plotmTF(mouseB)

Voltage and Hemo reconstruction : with mean alpha function

In [19]:
# alpha = mouseA.experiments[3].repeats[3].meanAlphaModel
alpha = mouseB.experiments[2].meanAlpha
gr.plotRModel(mouseA, exp = 2, rep = 4,alpha = alpha )
gr.plotHModel(mouseA, exp = 2, rep = 4,alpha = alpha )
In [20]:
alpha = mouseB.experiments[0].repeats[2].meanAlphaModel
gr.plotRModel(mouseA, exp = 2, rep = 4,alpha = alpha )
gr.plotHModel(mouseA, exp = 2, rep = 4,alpha = alpha )

Voltage and Hemo reconstruction : with mean alpha model function

In [21]:
# alpha = mouseA.experiments[3].repeats[3].meanAlphaModel
alpha = mouseB.experiments[2].meanAlphaModel
gr.plotRModel(mouseA, exp = 2, rep = 4,alpha = alpha )
gr.plotHModel(mouseA, exp = 2, rep = 4,alpha = alpha )

Distribution of alpha model parameters $\tau_0, \tau_1, \tau_2, \tau_3$

$v * \alpha = h$

$\alpha(t)=\frac{\tau_0}{t+0.1} - \frac{t}{\tau_1}*e^{-\frac{t}{\tau_2}}$

In [22]:
alpha = mouseB.experiments[2].meanAlphaModel
fontsize=16
plt.plot(xax(alpha,60000),alpha)
plt.xlabel('Time [s]')
plt.title('Alpha model of mean TF mouse B, exp %d'%(2+mouseB.start))
plt.text(30,-0.01, r'$ \tau_{0} = %.2g$' %mouseB.experiments[2].meanAlphaParams[0], fontsize =fontsize)
plt.text(30,-0.02, r'$ \tau_{1} = %.2g$' %mouseB.experiments[2].meanAlphaParams[1], fontsize =fontsize)
plt.text(30,-0.03, r'$ \tau_{2} = %.2g$' %mouseB.experiments[2].meanAlphaParams[2], fontsize =fontsize)
Out[22]:
<matplotlib.text.Text at 0x1d6b6f550>
In [23]:
gr.computeCorr(mouseA, alpha=None)
gr.computeCorr(mouseB, alpha=None)
gr.computeAlphaModels(mouseA)
gr.computeAlphaModels(mouseB)
gr.plotParamsIndex(mouseA)
gr.plotParamsIndex(mouseB)
gr.plotParamsCor(mouseA)
gr.plotParamsCor(mouseB)
print([meanCorR(mouseA),meanCorH(mouseA),meanCorR(mouseB), meanCorH(mouseB)])
[0.19648879065063474, 0.30145650502717064, 0.3341432660619974, 0.25650315342969893]

Optimize for hemo

In [40]:
resultHemo = minimize(fnH, method = 'COBYLA', x0=[1e-2,1,5],
                      options={'gtol': 1e-6, 'disp': True})
print(resultHemo)
  status: 1
    nfev: 224
   maxcv: 0.0
 success: True
     fun: 0.63045811254348161
       x: array([ 0.93304355,  0.57849867,  1.52215304])
 message: 'Optimization terminated successfully.'
In [41]:
p_est = resultHemo.x
alpha = [guess_function(xi,p_est[0],p_est[1],p_est[2]) for xi in x]
plt.plot(xax(alpha,60000),alpha)
# alpha2 = mouseB.experiments[2].meanAlphaModel
# plt.plot(xax(alpha2,60000),alpha2)
Out[41]:
[<matplotlib.lines.Line2D at 0x1f529f310>]
In [39]:
# computeAndPlotCorr(mouseA, alpha)
gr.plotHModel(mouseA,2,4,alpha)
gr.plotRModel(mouseA,2,4,alpha)
gr.plotHModel(mouseA,2,4,mouseB.experiments[2].meanAlphaModel)
gr.plotRModel(mouseA,2,4,mouseB.experiments[2].meanAlphaModel)
def printl(tab):
    for t in tab:
        print(t)

Optimize for voltage

In [31]:
# bounds = [(-10,10),(-10,10),(-10,10),(-10,10)]
# resultVolt = differential_evolution(fn, bounds, init = 'random')
resultVolt = minimize(fn, method = 'COBYLA', x0=[-1e-2,1,10,1],
                      options={'gtol': 1e-6, 'disp': True})
print(resultVolt)
  status: 2
    nfev: 1000
   maxcv: 0.0
 success: False
     fun: 0.62676022060598546
       x: array([ 0.27171972,  0.99757237,  3.87773108,  1.18657255])
 message: 'Maximum number of function evaluations has been exceeded.'
In [32]:
p_est = resultVolt.x
print(p_est)
alpha = [guess_function(xi,p_est[0],p_est[1],p_est[2]) for xi in x]
[ 0.27171972  0.99757237  3.87773108  1.18657255]
In [33]:
plt.plot(xax(alpha,60000),alpha)
Out[33]:
[<matplotlib.lines.Line2D at 0x1d253fb90>]
In [34]:
gr.plotRModel(mouseA,2,4,alpha)
gr.plotHModel(mouseA,2,4,alpha)
In [35]:
# gr.computeCorr(mouseA, alpha = alpha)
# gr.computeCorr(mouseB, alpha = alpha)
# gr.plotParamsIndex(mouseA)
# gr.plotParamsIndex(mouseB)
# gr.plotParamsCor(mouseA)
# gr.plotParamsCor(mouseB)
# print([meanCorR(mouseA),meanCorH(mouseA),meanCorR(mouseB), meanCorH(mouseB)])
In [ ]: